package com.castle.fortress.admin.study.course.rest;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.study.course.entity.StudyCourseChapterCoursewareEntity;
import com.castle.fortress.admin.study.course.dto.StudyCourseChapterCoursewareDto;
import com.castle.fortress.admin.system.entity.SysUser;
import com.castle.fortress.admin.study.course.service.StudyCourseChapterCoursewareService;
import com.castle.fortress.common.utils.ConvertUtil;
import com.castle.fortress.admin.utils.WebUtil;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.enums.OperationTypeEnum;
import com.castle.fortress.admin.core.annotation.CastleLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.util.Strings;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 章节课件表 api 控制器
 *
 * @author castle
 * @since 2023-01-31
 */
@Api(tags="章节课件表api管理控制器")
@RestController
@RequestMapping("/api/study/course/studyCourseChapterCourseware")
public class ApiStudyCourseChapterCoursewareController {
    @Autowired
    private StudyCourseChapterCoursewareService studyCourseChapterCoursewareService;


    /**
     * 章节课件表的分页展示
     * @param studyCourseChapterCoursewareDto 章节课件表实体类
     * @param currentPage 当前页
     * @param size  每页记录数
     * @return
     */
    @CastleLog(operLocation="章节课件表-api",operType = OperationTypeEnum.QUERY)
    @ApiOperation("章节课件表分页展示")
    @GetMapping("/page")
    public RespBody<IPage<StudyCourseChapterCoursewareDto>> pageStudyCourseChapterCourseware(StudyCourseChapterCoursewareDto studyCourseChapterCoursewareDto, @RequestParam(required = false) Integer currentPage, @RequestParam(required = false)Integer size){
        Integer pageIndex= currentPage==null? GlobalConstants.DEFAULT_PAGE_INDEX:currentPage;
        Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
        Page<StudyCourseChapterCoursewareDto> page = new Page(pageIndex, pageSize);

        IPage<StudyCourseChapterCoursewareDto> pages = studyCourseChapterCoursewareService.pageStudyCourseChapterCourseware(page, studyCourseChapterCoursewareDto);
        return RespBody.data(pages);
    }

    /**
     * 章节课件表保存
     * @param studyCourseChapterCoursewareDto 章节课件表实体类
     * @return
     */
    @CastleLog(operLocation="章节课件表-api",operType = OperationTypeEnum.INSERT)
    @ApiOperation("章节课件表保存")
    @PostMapping("/save")
    public RespBody<String> saveStudyCourseChapterCourseware(@RequestBody StudyCourseChapterCoursewareDto studyCourseChapterCoursewareDto){
        if(studyCourseChapterCoursewareDto == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            StudyCourseChapterCoursewareEntity studyCourseChapterCoursewareEntity = ConvertUtil.transformObj(studyCourseChapterCoursewareDto,StudyCourseChapterCoursewareEntity.class);
        if(studyCourseChapterCoursewareService.save(studyCourseChapterCoursewareEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 章节课件表编辑
     * @param studyCourseChapterCoursewareDto 章节课件表实体类
     * @return
     */
    @CastleLog(operLocation="章节课件表-api",operType = OperationTypeEnum.UPDATE)
    @ApiOperation("章节课件表编辑")
    @PostMapping("/edit")
    public RespBody<String> updateStudyCourseChapterCourseware(@RequestBody StudyCourseChapterCoursewareDto studyCourseChapterCoursewareDto){
        if(studyCourseChapterCoursewareDto == null || studyCourseChapterCoursewareDto.getId() == null || studyCourseChapterCoursewareDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            StudyCourseChapterCoursewareEntity studyCourseChapterCoursewareEntity = ConvertUtil.transformObj(studyCourseChapterCoursewareDto,StudyCourseChapterCoursewareEntity.class);
        if(studyCourseChapterCoursewareService.updateById(studyCourseChapterCoursewareEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 章节课件表删除
     * @param ids 章节课件表id集合
     * @return
     */
    @CastleLog(operLocation="章节课件表-api",operType = OperationTypeEnum.DELETE)
    @ApiOperation("章节课件表删除")
    @PostMapping("/delete")
    public RespBody<String> deleteStudyCourseChapterCourseware(@RequestBody List<Long> ids){
        if(ids == null || ids.isEmpty()){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(studyCourseChapterCoursewareService.removeByIds(ids)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 章节课件表详情
     * @param id 章节课件表id
     * @return
     */
    @CastleLog(operLocation="章节课件表-api",operType = OperationTypeEnum.QUERY)
    @ApiOperation("章节课件表详情")
    @GetMapping("/info")
    public RespBody<StudyCourseChapterCoursewareDto> infoStudyCourseChapterCourseware(@RequestParam Long id){
        if(id == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            StudyCourseChapterCoursewareEntity studyCourseChapterCoursewareEntity = studyCourseChapterCoursewareService.getById(id);
            StudyCourseChapterCoursewareDto studyCourseChapterCoursewareDto = ConvertUtil.transformObj(studyCourseChapterCoursewareEntity,StudyCourseChapterCoursewareDto.class);
        return RespBody.data(studyCourseChapterCoursewareDto);
    }


}
